package com.sunny.controller.backend;

import ch.qos.logback.core.net.SyslogOutputStream;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sunny.common.Const;
import com.sunny.common.ServerResponse;
import com.sunny.pojo.User;
import com.sunny.service.IUserService;
import com.sunny.service.impl.UserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by yubao.jin on 2018/3/7.
 */
@Controller
@RequestMapping("/manage/user")
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private IUserService iUserService;

    @RequestMapping(value = "login.do")
    @ResponseBody
    public ModelAndView login(String domainAccount, HttpSession session) {
        ServerResponse<User> serverResponse = iUserService.login(domainAccount);
        if(serverResponse.isSuccess()) {
            session.setAttribute(Const.CURRENT_USER, serverResponse.getData());
        }
        ServerResponse<PageInfo<User>> serverResponseList = iUserService.userList(1, 10, null, null, null);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("userList", serverResponseList.getData().getList());
        modelAndView.addObject("pageInfo", serverResponseList.getData());
        modelAndView.setViewName("user");
        return modelAndView;
    }

    @RequestMapping(value = "list.do")
    @ResponseBody
    public ServerResponse<PageInfo<User>> userList(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                             @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                             @RequestParam(value = "username", required = false) String username,
                                             @RequestParam(value = "role", required = false)String role,
                                             @RequestParam(value = "state", required = false) String state,
                                             HttpSession session){
        ServerResponse<PageInfo<User>> serverResponse = iUserService.userList(pageNum, pageSize,username, role, state);
        return serverResponse;
    }

    @RequestMapping("select_user.do")
    @ResponseBody
    public ServerResponse selectUser(int userId, HttpSession session) {
        if(checkUser(session) == null) {
            return ServerResponse.createByErrorMessage("用户未登录");
        }
        return iUserService.selectUserById(userId);
    }

    @RequestMapping(value = "update.do")
    @ResponseBody
    public ServerResponse updateUser(@RequestParam(value = "userId", required = true) String userId,
                                     @RequestParam(value = "role", required = false) String role,
                                     @RequestParam(value = "username", required = false) String username,
                                     @RequestParam(value = "phonNumber", required = false) String phonNumber,
                                           HttpSession session){
        if(checkUser(session) == null) {
            return ServerResponse.createByErrorMessage("用户未登录");
        }
        return iUserService.updateUser(userId, role, username, phonNumber);
    }

    @RequestMapping(value = "disable.do")
    @ResponseBody
    public ServerResponse disableUser(String userId, HttpSession session) {
        if(checkUser(session) == null) {
            return ServerResponse.createByErrorMessage("用户未登录");
        }
        return iUserService.disableUser(userId);
    }

    @RequestMapping(value = "enable.do")
    @ResponseBody
    public ServerResponse enableUser(String userId, HttpSession session) {
        if(checkUser(session) == null) {
            return ServerResponse.createByErrorMessage("用户未登录");
        }
        return iUserService.enableUser(userId);
    }

    @RequestMapping(value = "insert.do")
    @ResponseBody
    public ServerResponse enableUser(@RequestParam(value = "state", required = true) String state,
                                     @RequestParam(value = "role", required = false) String role,
                                     @RequestParam(value = "username", required = false) String username,
                                     @RequestParam(value = "phonNumber", required = false) String phonNumber,
                                     @RequestParam(value = "domainAccount", required = false) String domainAccount,
                                     HttpSession session){
        if(checkUser(session) == null) {
            return ServerResponse.createByErrorMessage("用户未登录");
        }

        return iUserService.insertUser(state,role,username,phonNumber,domainAccount);
    }

    public User checkUser(HttpSession session) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        return user;
    }
}
